package com.dc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.dc.dao.AdminDao;
import com.dc.entity.Admin;
import com.dc.entity.Users;
import com.dc.tools.DBTools;

public class AdminDaoImpl implements AdminDao {

	/**
	 * 登录管理员操作的模拟
	 * 
	 * @param name
	 * @param psw
	 * @return true 登录成功 false 登录失败
	 */
	@Override
	public boolean doLogin(String name, String psw) {
		// 完成登录验证的sql语句
		String sql = "select count(adminid) from admin where adminname=? and adminpsw = ?";
		// 得到连接对象
		Connection conn = DBTools.getConn();
		// 创建PreparedStatement对象
		PreparedStatement pst = null;
		ResultSet rs = null;
		int n = 0;

		try {
			pst = conn.prepareStatement(sql);
			// 设置？的值
			pst.setString(1, name);
			pst.setString(2, psw);
			// 执行sql
			rs = pst.executeQuery();
			// 处理结果
			if (rs.next()) {
				n = rs.getInt(1);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 释放资源
			try {
				rs.close();
				pst.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return n == 1;
	}
	
	@Override
	public Admin getAdminByNameAndPwd(String name, String psw) {
		// TODO Auto-generated method stub
		String sql = "select * from admin where adminname = ? and adminpsw= ?";
		List<Admin> list = (List<Admin>) DBTools.exQuery(sql, Admin.class,name,psw);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
}
